package com.globalwave.db.seq.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.globalwave.db.exception.DBException;
import com.globalwave.db.seq.Sequence;

public class TableSequence extends BaseSequence implements Sequence {

	private static final String TABLE = "COMMON_SEQUENCE" ;
	
	private String name_       ;
	private Long min_          ;
	private Long max_          ;
	private Short increment_by ;
	private Short cycle_flag   ;
	private Short order_flag   ;
	private Short cache_size   ;
	private Long last_number   ;
	
	public TableSequence(String name) {
		this.name_ = name ;
	}
	@Override
	public long currentVal() {
		return getLong("select last_number from " + TABLE + " where name_=?", new Object[]{name_}) ;
	}

	@Override
	public long nextVal() {

		PreparedStatement ps = null ;
		ResultSet rs = null ;
		Connection con = null ;
		try {
			con = getConnection() ; 
			con.setAutoCommit(false) ;
		    ps = con.prepareStatement("update " + TABLE + " set last_number=last_number+1 where name_=?") ;
		    ps.setString(1, name_) ;
		    
		    int updated = ps.executeUpdate() ;
		    
		    if (updated == 0) {
		    	con.rollback();
			    return -1 ;
		    } else {
				long result = getLong("select last_number from " + TABLE + " where name_=?", new Object[]{name_}) ;
				
				con.commit() ;
			    
			    return result ;
		    }
		} catch (Exception e) {
		    try {
		    	con.rollback();
			} catch (Exception e1) {
				throw new DBException(e1) ;
			}
			throw new DBException(e) ;
		} finally {
			if (ps != null) {
				try {
					ps.close() ;
				} catch (Exception e) {
					throw new DBException(e) ;
				}
			}
			if (rs != null) {
				try {
					rs.close() ;
				} catch (Exception e) {
					throw new DBException(e) ;
				}
			}
		}
	}

	@Override
	public long create() {

		PreparedStatement ps = null ;
		Connection con = null ;
		try {
			con = getConnection() ; 
			
			con.setAutoCommit(false) ;

		    ps = con.prepareStatement("insert into " + TABLE + " (name_, last_number) values(?, 1)") ;
		    ps.setString(1, name_) ;
		    
		    ps.executeUpdate() ;
		    
		    con.commit() ;
		    return 1 ;
		} catch (Exception e) {
		    try {
		    	con.rollback();
			} catch (Exception e1) {
				throw new DBException(e1) ;
			}
			throw new DBException(e) ;
		} finally {
			if (ps != null) {
				try {
					ps.close() ;
				} catch (Exception e) {
					throw new DBException(e) ;
				}
			}
		}
	}

	public String getName_() {
		return name_;
	}

	public void setName_(String name_) {
		this.name_ = name_;
	}

	public Long getMin_() {
		return min_;
	}

	public void setMin_(Long min_) {
		this.min_ = min_;
	}

	public Long getMax_() {
		return max_;
	}

	public void setMax_(Long max_) {
		this.max_ = max_;
	}

	public Short getIncrement_by() {
		return increment_by;
	}

	public void setIncrement_by(Short increment_by) {
		this.increment_by = increment_by;
	}

	public Short getCycle_flag() {
		return cycle_flag;
	}

	public void setCycle_flag(Short cycle_flag) {
		this.cycle_flag = cycle_flag;
	}

	public Short getOrder_flag() {
		return order_flag;
	}

	public void setOrder_flag(Short order_flag) {
		this.order_flag = order_flag;
	}

	public Short getCache_size() {
		return cache_size;
	}

	public void setCache_size(Short cache_size) {
		this.cache_size = cache_size;
	}

	public Long getLast_number() {
		return last_number;
	}

	public void setLast_number(Long last_number) {
		this.last_number = last_number;
	}
	
	

}
